package com.iterator.controller.web.spreadManager;

import com.iterator.bean.web.pojo.tcy.Ad;
import com.iterator.bean.web.stone.BaseRespVo;
import com.iterator.bean.web.tcy.AdVO;
import com.iterator.service.web.spreadManager.AdService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author tean_cy
 * @date 2021/1/10 15:51
 */

@RestController
@RequestMapping("admin/ad")
public class AdController {

    @Autowired
    AdService adService;

    /**
     * 广告管理模块：list接口
     */
    @RequestMapping("list")
    @RequiresPermissions("admin:ad:list")
    public BaseRespVo list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,
                           @Nullable @RequestParam("name") String name, @Nullable @RequestParam("content") String content,
                           @RequestParam("sort") String sort, @RequestParam("order") String order) {

        //TODO 需要对请求参数进行判断，非空的参数如果为空则需要抛异常

        AdVO adVO = adService.list(page, limit, name, content, sort, order);
        return BaseRespVo.ok(adVO);

    }

    /**
     * 广告管理模块：update接口
     */
    @RequestMapping("update")
    @RequiresPermissions("admin:ad:update")
    public BaseRespVo update(@RequestBody Ad adBO) {
        Ad adVO = adService.update(adBO);
        return BaseRespVo.ok(adVO);
    }

    /**
     * 广告管理模块：delete接口
     */
    @RequestMapping("delete")
    @RequiresPermissions("admin:ad:delete")
    public BaseRespVo delete(@RequestBody Ad adBO) {
        int code = adService.delete(adBO);
        if (code != 0){

        return BaseRespVo.ok();
        }
        return BaseRespVo.fail("删除失败");
    }

    /**
     * 广告管理模块：create接口
     */

    @RequestMapping("create")
    @RequiresPermissions("admin:ad:create")
    public BaseRespVo create(@RequestBody Ad adCreateBO){
        Ad advo = adService.create(adCreateBO);
        if (advo != null){
            return BaseRespVo.ok(advo);
        }
        return BaseRespVo.fail("添加失败");
    }
}
